package com.icecloud.cloud.upms.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.icecloud.cloud.upms.common.utils.RestfulResponseBody;
import com.icecloud.cloud.upms.entity.Department;
import com.icecloud.cloud.upms.service.DepartmentService;

/**
 * 部门ctr
 * 
 * @author penghaozhong
 *
 */
@RestController
public class DepartmentController {

	@Autowired
	private DepartmentService departmentService;

	/**
	 * 根据部门id查询
	 * 
	 * @param departmentId
	 * @return
	 */
	@RequestMapping("/department/{departmentId}")
	public ResponseEntity<RestfulResponseBody> get(@PathVariable int departmentId) {
		RestfulResponseBody body = new RestfulResponseBody();
		body.put("data", departmentService.get(departmentId));
		body.setReason("成功");
		body.setResultFlag(1);
		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}

	/**
	 * 根据部门id删除
	 * 
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value = "/department/{departmentId}", method = RequestMethod.DELETE)
	public ResponseEntity<RestfulResponseBody> delete(@PathVariable int departmentId) {
		RestfulResponseBody body = new RestfulResponseBody();
		if (departmentService.delete(departmentId) > 0) {
			body.setReason("成功");
			body.setResultFlag(1);
		} else {
			body.setReason("失败");
			body.setResultFlag(8001);
		}
		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}
	
	/**
	 * 新增一个部门
	 * 
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value = "/department", method = RequestMethod.POST)
	public ResponseEntity<RestfulResponseBody> save(@RequestBody Department department) {
		RestfulResponseBody body = new RestfulResponseBody();
		
		try {
			if (departmentService.save(department) > 0) {
				body.setReason("成功");
				body.setResultFlag(1);
			} else {
				body.setReason("失败");
				body.setResultFlag(801);
			}
		} catch (DataAccessException e) {
			body.setReason("保存失败：部门名称已存在！");
			body.setResultFlag(8012);
		}
		
		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}
	
	/**
	 * 修改一个部门
	 * 
	 * @param departmentId
	 * @return
	 */
	@RequestMapping(value = "/department", method = RequestMethod.PATCH)
	public ResponseEntity<RestfulResponseBody> update(@RequestBody Department department) {
		RestfulResponseBody body = new RestfulResponseBody();
		if (departmentService.update(department) > 0) {
			body.setReason("成功");
			body.setResultFlag(1);
		} else {
			body.setReason("失败");
			body.setResultFlag(8001);
		}
		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}
	
	/**
	 * 查询部门列表可根据部门名称查询
	 * @param departName
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/departments")
	public ResponseEntity<RestfulResponseBody> list(String departName,@RequestParam(defaultValue="1") int pageNum, @RequestParam(defaultValue="4") int pageSize) {
		RestfulResponseBody body = new RestfulResponseBody();
		body.put("data", departmentService.list(departName, pageNum, pageSize));
		body.setReason("成功");
		body.setResultFlag(1);
		return new ResponseEntity<RestfulResponseBody>(body, HttpStatus.OK);
	}

}
